package com.jincp.image.load;

import android.graphics.Color;
import android.support.annotation.DrawableRes;

/**
 * Created by admin on 2017/3/23.
 */

public class ImageLoaderOptions {

    private static int DEFAULT_DRAWABLE;
    private static int ERROR_DRAWABLE;

    public static final int SHAPE_DEFAULT = 0;
    public static final int SHAPE_CIRCLE = 1;
    public static final int SHAPE_ROUND = 2;

    public enum CornerType {
        /**
         * 所有角
         */
        ALL,
        /**
         * 左上
         */
        LEFT_TOP,
        /**
         * 左下
         */
        LEFT_BOTTOM,
        /**
         * 右上
         */
        RIGHT_TOP,
        /**
         * 右下
         */
        RIGHT_BOTTOM,
        /**
         * 左侧
         */
        LEFT,
        /**
         * 右侧
         */
        RIGHT,
        /**
         * 下侧
         */
        BOTTOM,
        /**
         * 上侧
         */
        TOP,
    }

    public static void init(@DrawableRes int defaultDrawable, @DrawableRes int errorDrawable) {
        ImageLoaderOptions.DEFAULT_DRAWABLE = defaultDrawable;
        ImageLoaderOptions.ERROR_DRAWABLE = errorDrawable;
    }

    private int reWidth; // 重新设定容器宽高
    private int reHeight; // 重新设定容器宽高
    private int defaultDrawable; // 当没有成功加载的时候显示的图片
    private int errorDrawable;  // 加载错误的时候显示的drawable
    private float imageShape;  //加载的时候形状
    private float borderSize;  //加载的时候圆形图片边框
    private int borderColor;  //加载的时候圆形图片边框
    private float roundRadius;  //加载的时候圆角图片的圆角大小
    private CornerType roundType;  //加载的时候圆形图片的样式

    private ImageLoaderOptions(Builder builder) {
        this.defaultDrawable = builder.defaultDrawable;
        this.reWidth = builder.reWidth;
        this.reHeight = builder.reHeight;
        this.errorDrawable = builder.errorDrawable;
        this.imageShape = builder.imageShape;
        this.borderSize = builder.borderSize;
        this.borderColor = builder.borderColor;
        this.roundRadius = builder.roundRadius;
        this.roundType = builder.roundType;
    }

    public int getReWidth() {
        return reWidth;
    }

    public int getReHeight() {
        return reHeight;
    }

    public int getDefaultDrawable() {
        return defaultDrawable;
    }

    public int getErrorDrawable() {
        return errorDrawable;
    }

    public float getImageShape() {
        return imageShape;
    }

    public float getBorderSize() {
        return borderSize;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public float getRoundRadius() {
        return roundRadius;
    }

    public CornerType getRoundType() {
        return roundType;
    }

    public static ImageLoaderOptions getCircleOptions() {
        return getCircleOptions(0, 0, 0);
    }

    public static ImageLoaderOptions getCircleOptions(@DrawableRes int placeResource, float borderSize, int borderColor) {
        ImageLoaderOptions.Builder builder = new Builder();
        builder.setImageShape(SHAPE_CIRCLE, borderSize, borderColor, 0, null);
        if (placeResource != 0) {
            builder.setDefaultDrawable(placeResource);
            builder.setErrorDrawable(placeResource);
        }
        return builder.build();
    }

    public static ImageLoaderOptions getRoundOptions() {
        return getRoundOptions(10, CornerType.ALL);
    }

    public static ImageLoaderOptions getRoundOptions(float roundRadius, CornerType cornerType) {
        return getRoundOptions(0, 0, 0, roundRadius, cornerType);
    }

    public static ImageLoaderOptions getRoundOptions(@DrawableRes int placeResource, float borderSize, int borderColor, float roundRadius, CornerType cornerType) {
        ImageLoaderOptions.Builder builder = new Builder();
        if (roundRadius > 0) {
            builder.setImageShape(SHAPE_ROUND, borderSize, borderColor, roundRadius, cornerType);
        }
        if (placeResource != 0) {
            builder.setDefaultDrawable(placeResource);
            builder.setErrorDrawable(placeResource);
        }
        return builder.build();
    }

    public static final class Builder {
        private int reWidth = 0;
        private int reHeight = 0;
        private int defaultDrawable = DEFAULT_DRAWABLE;
        private int errorDrawable = ERROR_DRAWABLE;
        private float imageShape = DEFAULT_DRAWABLE;  //加载的时候形状
        private float roundRadius = 10;  //加载的时候圆角图片的圆角大小
        private float borderSize = 0;  //加载的时候圆形图片边框
        private int borderColor = Color.TRANSPARENT;  //加载的时候圆形图片边框
        private CornerType roundType = CornerType.ALL;  //加载的时候圆形图片的样式

        public Builder setDefaultDrawable(int defaultDrawable) {
            this.defaultDrawable = defaultDrawable;
            return this;
        }

        public Builder reSize(int reWidth, int reHeight) {
            this.reHeight = reHeight;
            this.reWidth = reWidth;
            return this;
        }

        public Builder setImageShape(int imageShape, float borderSize, int borderColor, float roundRadius, CornerType roundtype) {
            this.imageShape = imageShape;
            this.borderSize = borderSize;
            this.borderColor = borderColor;
            this.roundRadius = roundRadius;
            this.roundType = roundtype;
            return this;
        }

        public Builder setErrorDrawable(int errorDrawable) {
            this.errorDrawable = errorDrawable;
            return this;
        }

        public ImageLoaderOptions build() {
            return new ImageLoaderOptions(this);
        }
    }

}
